package com.tbit.main.controller;

import com.tbit.main.common.JsonResult;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.main.constant.OperateLog;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.CommonFunction;
import com.tbit.main.service.CommonFunctionService;
import com.tbit.main.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 用户常用功能
 *
 * @Author: chen
 * @Description:
 * @Date: 创建于 15:13 2023/2/20
 */
@RestController
@RequestMapping("/commonFunction")
public class CommonFunctionController {

    @Autowired
    private CommonFunctionService commonFunctionService;
    @Autowired
    private TokenService tokenService;

    /**
     * 添加
     *
     * @param functionName 功能名称
     * @param token
     * @return
     */
    @RequestMapping("/addBatch")
    @OperateLog(module = ModuleEnum.COMMONFUNCTION, methods = MethodEnum.COMMONFUNCTION_ADD)
    public JsonResult addBatch(String functionName, String token) {
        AccountUser accountUser = tokenService.getAccount(token);
        List<String> list = new ArrayList<>();
        if (functionName != null && !"".equals(functionName)) {
            list = Arrays.asList(functionName.split(","));
        }
        commonFunctionService.add(list, accountUser);
        return JsonResult.succ();
    }

    /**
     * 获取用户常用配置
     *
     * @param token
     * @return
     */
    @RequestMapping("/getUserFunction")
    public JsonResult getUserFunction(String token) {
        AccountUser accountUser = tokenService.getAccount(token);
        List<CommonFunction> list = commonFunctionService.getUserFunction(accountUser.getAccountUserId());
        return JsonResult.succ(list);
    }
}
